//
//  SingupBaoMingPepole.m
//  HappyStore
//
//  Created by 李小辉 on 16/6/14.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "SingupBaoMingPepole.h"
#import "SingUpCell.h"
@interface SingupBaoMingPepole ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *aryInfo;

@property (assign, nonatomic) NSInteger paggge;

@property (strong, nonatomic) NSMutableDictionary *muDicParameters;
@end

@implementation SingupBaoMingPepole


- (NSMutableArray *)aryInfo{
    if (!_aryInfo) {
        _aryInfo = [NSMutableArray array];
    }
    return _aryInfo;
}
- (NSMutableDictionary *)muDicParameters{
    if (!_muDicParameters) {
        _muDicParameters = [NSMutableDictionary dictionary];
    }
    return _muDicParameters;
}



- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = @"已报名";
    titleLabel.textColor = RGB(45, 45, 45);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    
    
    // 设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.paggge = 1;
    
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.paggge += 1;
        [self.muDicParameters setObject:[NSString stringWithFormat:@"%ld",self.paggge] forKey:@"page"];
        [self startNetWork];
    }];
    
    self.tableView.mj_footer.hidden = YES;
    
    if (self.muDicParameters == nil) {
        self.muDicParameters = [NSMutableDictionary dictionary];
        [self.muDicParameters setObject:@"1" forKey:@"page"];
    }

    [self startNetWork];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}


- (void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -请求任务
- (void)startNetWork
{
    [self showRoundProgressWithTitle:@"请稍后..."];
    
    NSMutableDictionary *muDicParameters = [NSMutableDictionary dictionary];
    
    [muDicParameters setObject:self.activityId forKey:@"activityId"];
    
    NSString *strURl = [NSString stringWithFormat:@"%@%@",URL_DEFINE,@"signupMore_app_activity.im"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:strURl parameters:muDicParameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        //NSLog(@"%@",dicInfo);

        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
            
            if (self.paggge == 1) {
                [self.tableView.mj_header endRefreshing];
                
                
                self.aryInfo = [NSMutableArray arrayWithArray:[dicInfo objectForKey:@"data"]];
                
                
                if (self.aryInfo.count == 10) {
                    self.tableView.mj_footer.hidden = NO;
                }else {
                    self.tableView.mj_footer.hidden = YES;
                }
                
            }else {
                [self.tableView.mj_footer endRefreshing];
                NSArray *ary11 = [dicInfo objectForKey:@"data"][@"record"];
                for (int i = 0; i<ary11.count; i++) {
                    NSDictionary *dic1 = ary11[i];
                    [_aryInfo addObject:dic1];
                }
                if (ary11.count == 10) {
                    self.tableView.mj_footer.hidden = NO;
                }else {
                    self.tableView.mj_footer.hidden = YES;
                }
                
            }
            
            
            [self.tableView reloadData];
            [self hideBubble];

        }else {
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];

            [self showSingleIconBubble:[dicInfo objectForKey:@"msg"]];

        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        [self showSingleIconBubble:error.localizedDescription];

    }];
}




#pragma mark - tableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    [tableView tableViewDisplayWitMsg:@"没有查询到相对应的数据" ifNecessaryForRowCount:_aryInfo.count];
    return self.aryInfo.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *strCell = @"activityCell";
    UINib *nib = [UINib nibWithNibName:@"SingUpCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:strCell];
    SingUpCell *cell = [self.tableView dequeueReusableCellWithIdentifier:strCell];
    
    if (!cell) {
        cell = [[SingUpCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
    }
    NSDictionary *dic = self.aryInfo[indexPath.row];

    if (dic[@"posiTionName"]==nil||[dic[@"posiTionName"] isEqualToString:@"null"] ) {
        cell.posiTion.text = @"";
    }else{
        cell.posiTion.text = dic[@"posiTionName"];
    }
    [cell.headPhotoImageView sd_setImageWithURL:dic[@"headPhoto"]];
    cell.headPhotoImageView.layer.cornerRadius = 23.0f;
    cell.headPhotoImageView.layer.masksToBounds = YES;
    
    
    if (dic[@"userName"]==nil||[dic[@"userName"] isEqualToString:@""]||[dic[@"userName"] isEqualToString:@"null"]) {
        cell.userName.text = dic[@"account"];
    }else{
        cell.userName.text = dic[@"userName"];
    }
    
  
    cell.authStateImageView.image = [UsedTool setVipimage:dic[@"vipLevel"]];
    //认证
    cell.isAuthState.hidden = [UsedTool authStateImageViewHidden:dic[@"authState"]];
    //判断上级
    cell.companyUserName.text = [UsedTool userType:dic[@"userType"] companyName:dic[@"companyName"] userAddress:dic[@"userAddress"]];

    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}



@end
